/*
 * Copyright (c) United Binary LLC.  All rights reserved.
 * 
 * This code is licensed under the MIT License
 * 
 * SEE: http://harnessit.codeplex.com/license
 * 
 */
#region using ...
using System;
using System.Drawing;
#endregion

namespace UnitedBinary.Core.Windows.Forms
{
	/// <include file='Forms.xml' path='/Docs/RichTextString/Class/*'/>
	public class RichTextString : IDisposable
	{
		private System.Drawing.Font font = null;
		private string text = null;
		private Color color = Color.Black;
		private bool modified = true;
		private bool ownsFont = false;

		/// <include file='Forms.xml' path='/Docs/RichTextString/ctor/*'/>
		public RichTextString()
		{
			this.font = new System.Drawing.Font( "Microsoft Sans Serif", 12 );
			this.text = string.Empty;
			ownsFont = true;
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/ctor_string_font/*'/>
		public RichTextString(string text, System.Drawing.Font font)
		{
			this.text = text;
			this.font = font;
			ownsFont = false;
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/ctor_string_color_font/*'/>
		public RichTextString(string text, System.Drawing.Color color, System.Drawing.Font font)
		{
			this.text = text;
			this.font = font;
			this.color = color;
			ownsFont = false;
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/ctor_string/*'/>
		public RichTextString(string text)
		{
			this.text = text;
			this.font = new System.Drawing.Font( "Microsoft Sans Serif", 12 );
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/ctor_string_color/*'/>
		public RichTextString(string text, System.Drawing.Color color)
		{
			this.text = text;
			this.color = color;
			this.font = new System.Drawing.Font( "Microsoft Sans Serif", 12 );
		}

		/// <summary />
		~RichTextString()
		{
			Dispose( false );
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/Dispose/*'/>
		public void Dispose()
		{
			Dispose( true );
			GC.SuppressFinalize( this );
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/Dispose_bool/*'/>
		protected virtual void Dispose(bool disposing)
		{
			if (disposing && ownsFont)
			{
				using (font)
				{
					font = null;
				}
			}
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/Font/*'/>
		public System.Drawing.Font Font
		{
			get
			{
				return font;
			}
			set
			{
				font = value;
				Modified = true;
				ownsFont = false;
			}
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/Text/*'/>
		public string Text
		{
			get
			{
				return text;
			}
			set
			{
				Modified = true;
				if (value == null)
				{
					text = string.Empty;
				}
				else
				{
					text = value;
				}
			}
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/Length/*'/>
		public int Length
		{
			get
			{
				return text.Length;
			}
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/Modified/*'/>
		public bool Modified
		{
			get
			{
				return modified;
			}
			set
			{
				modified = value;
			}
		}

		#region Color Attributes
		/// <include file='Forms.xml' path='/Docs/RichTextString/Color/*'/>
		public Color Color
		{
			get
			{
				modified = true;
				return color;
			}
			set
			{
				color = value;
			}
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/R/*'/>
		public int R
		{
			set
			{
				modified = true;
				int r = System.Math.Max( 0, System.Math.Min(255, value) );
				color = Color.FromArgb( r, color.G, color.B );
			}
			get
			{
				return color.R;
			}
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/G/*'/>
		public int G
		{
			set
			{
				modified = true;
				int g = System.Math.Max( 0, System.Math.Min(255, value) );
				color = Color.FromArgb( color.R, g, color.B );
			}
			get
			{
				return color.G;
			}
		}

		/// <include file='Forms.xml' path='/Docs/RichTextString/B/*'/>
		public int B
		{
			set
			{
				modified = true;
				int b = System.Math.Max( 0, System.Math.Min(255, value) );
				color = Color.FromArgb( color.R, color.G, b );
			}
			get
			{
				return color.B;
			}
		}
		#endregion
	}
}
